//
//  HSPMarketViewController.m
//  Icar
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPMarketViewController.h"
#import "BaseHeader.h"
#import "AFNBlock.h"
@interface HSPMarketViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableViewMarket;
@property (nonatomic,assign)BOOL isUp;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)NSMutableArray *markArrayName;
@end

@implementation HSPMarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor: [UIColor whiteColor]];
    _markArrayName = [[NSMutableArray alloc]init];
    [self creatTabel];
    [self AlbumaHeader];
    [self AlbumaFooter];
}
- (void)creatTabel
{
    _tableViewMarket = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 147) style:UITableViewStylePlain];
    _tableViewMarket.delegate = self;
    _tableViewMarket.dataSource = self;
    [self.view addSubview:_tableViewMarket];
}
#pragma 上拉刷新
- (void)AlbumaHeader
{
    __block HSPMarketViewController *vc = self;
    [_tableViewMarket addLegendHeaderWithRefreshingBlock:^{
        vc.page = 1;
        [vc APPadndatanumber :1];
        _isUp = YES;
    }];
    [_tableViewMarket.header beginRefreshing];
}
#pragma 下拉加载
- (void)AlbumaFooter
{
    __block HSPMarketViewController *vcc = self;
    [_tableViewMarket addLegendFooterWithRefreshingBlock:^{
        vcc.page = vcc.page + 1;
        [vcc APPadndatanumber:vcc.page];
        _isUp = NO;
    }];
}
- (void)APPadndatanumber:(NSInteger)number
{
    if ([Reachability isConnectedInternet] == YES) {
        [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/cms/channels/10?pageNo=%ld&pageSize=20&v=4.0.0",_page] block:^(id back) {
            if (_isUp == YES) {
                [self.markArrayName removeAllObjects];
            }
            for (NSDictionary *dic in [back objectForKey:@"data"]) {
                HSPNewCarModel *model = [HSPNewCarModel modelWithDictionary:dic];
                [self.markArrayName addObject:model];
                
            }
            [self.tableViewMarket reloadData];
            [self.tableViewMarket.header endRefreshing];
            [self.tableViewMarket.footer endRefreshing];
        }];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _markArrayName.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSPFirstCell*cell = [tableView dequeueReusableCellWithIdentifier:@"mark"];
    if (cell == nil) {
        cell = [[HSPFirstCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"mark"];
    }
    HSPNewCarModel *model = self.markArrayName[indexPath.row];
    self.tableViewMarket.scrollsToTop = YES;
    [cell getCarImage:model];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
